package com.example.timeplanning_teacher;

import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class PassSingleThread extends Thread{
    private int task_id;
    private Toast Success;
    private Toast Failure;
    private Toast timeOut_Toast;

    public PassSingleThread(int task_id, Toast success, Toast failure, Toast timeOut_Toast) {
        this.task_id = task_id;
        Success = success;
        Failure = failure;
        this.timeOut_Toast = timeOut_Toast;
    }

    public void run(){
        try {
            String json = "{\n" +
                    "  \"task_id\": \"" + task_id + "\"\n" +
                    "}";
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url("http://58.58.36.198:9900/task/passSingle")
                    .post(RequestBody.create(MediaType.parse("application/json"),json))
                    .build();
            Response response = client.newCall(request).execute();
            String responseData = response.body().string();
            JSONObject jsonObject= JSONObject.parseObject(responseData);
            int code=(int)jsonObject.get("code");
            if(code == 100){
                Success.show();
            }
            else{
                Failure.show();
            }
        }catch (Exception e){
            e.printStackTrace();
            timeOut_Toast.show();
        }
    }
}
